package hsp.admin.app.literature.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hsp.admin.app.literature.aop.UpCountDelete;
import hsp.admin.app.literature.enums.LiteratureType;
import hsp.admin.app.literature.mapper.CelebrityMapper;
import hsp.admin.app.literature.po.CelebrityPO;
import hsp.admin.app.literature.service.ICelebrityService;
import hsp.admin.app.literature.vo.CelebrityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.io.Serializable;

@Service
public class CelebrityServiceImpl extends ServiceImpl<CelebrityMapper, CelebrityPO> implements ICelebrityService {

    private final CelebrityMapper mapper;

    @Autowired
    public CelebrityServiceImpl(
        CelebrityMapper mapper
    ){
        this.mapper = mapper;
    }

    private CelebrityPO getByName(String name){
        LambdaQueryWrapper<CelebrityPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CelebrityPO::getName, name);
        return getOne(wrapper);
    }

    @Override
    public boolean saveOrUpdate(CelebrityPO po) {
        if(po.getId() == null){
            Assert.isNull(getByName(po.getName()), "名称重复");
        }
        return super.saveOrUpdate(po);
    }

    @Override
    public Page<CelebrityVO> pageVO(Page<CelebrityVO> page, CelebrityPO po) {
        return mapper.selectPageVO(page, po);
    }

    @Override
    @UpCountDelete(LiteratureType.Celebrity)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

}
